package com.ysy.exception;


import com.ysy.enums.ResponseCodeEnum;
import com.ysy.persimmons.common.exception.ServiceException;
import com.ysy.persimmons.common.response.Response;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalException {

    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);

    /**
     * 业务异常
     * @param e
     */
    @ResponseBody
    @ExceptionHandler({ ServiceException.class })
    public Response<?> handleServiceException(HttpServletRequest request, ServiceException e) {
        logger.warn("request fail url:{}, errorCode: {}, errorMessage: {}"
                ,request.getRequestURI()
                ,e.getErrorCode()
                ,e.getErrorMessage());
        return Response.fail(e);
    }

    /**
     * 未知异常
     */
    @ResponseBody
    @ExceptionHandler({ Exception.class })
    public Response<Object> handleException(HttpServletRequest request, Exception e) {
        logger.error("request url:{}, error: ", request.getRequestURI(), e);
        return Response.fail(ResponseCodeEnum.SYSTEM_ERROR);
    }





}
